package handler

import (
	"blog_go/internal/model"
	"blog_go/internal/service"
	"blog_go/pkg/helper/resp"
	"net/http"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type CollectionHandler interface {
	QueryCollection(ctx *gin.Context)
	AddCollection(ctx *gin.Context)
	UpdateCollection(ctx *gin.Context)
	DelCollection(ctx *gin.Context)
}

type collectionHandler struct {
	*Handler
	collectionService service.CollectionService
}

func NewCollectionHandler(handler *Handler, collectionService service.CollectionService) CollectionHandler {
	return &collectionHandler{
		Handler:           handler,
		collectionService: collectionService,
	}
}

// 查询用户收藏夹
func (h *collectionHandler) QueryCollection(ctx *gin.Context) {
	userId := GetUserIdFromCtx(ctx)
	if userId == 0 {
		resp.HandleError(ctx, http.StatusBadRequest, 1, "token错误", nil)
		return
	}

	h.logger.Info("QueryCollection attempted", zap.Any("userId", userId))

	data, err := h.collectionService.QueryCollection(userId)

	if err != nil {
		h.logger.Error("QueryCollection failed", zap.Any("error", err))
		resp.HandleError(ctx, http.StatusInternalServerError, 1, err.Error(), nil)
		return
	}

	resp.HandleSuccess(ctx, data)
}

// 新增用户收藏夹
func (h *collectionHandler) AddCollection(ctx *gin.Context) {
	var collection model.Collection

	if err := ctx.ShouldBind(&collection); err != nil {
		h.logger.Warn("AddCollection", zap.Any("error", err))
		resp.HandleError(ctx, http.StatusBadRequest, 1, err.Error(), nil)
		return
	}

	h.logger.Info("AddCollection attempted", zap.Any("userId", collection.UserId))

	err := h.collectionService.AddCollection(collection)

	if err != nil {
		h.logger.Error("AddCollection failed", zap.Any("userId", collection.UserId), zap.Any("CollectionName", collection.CollectionName), zap.Any("error", err))
		resp.HandleError(ctx, http.StatusInternalServerError, 1, err.Error(), nil)
		return
	}

	resp.HandleSuccess(ctx, nil)
}

// 编辑收藏夹
func (h *collectionHandler) UpdateCollection(ctx *gin.Context) {
	var collection model.Collection

	if err := ctx.ShouldBind(&collection); err != nil {
		h.logger.Warn("UpdateCollection", zap.Any("error", err))
		resp.HandleError(ctx, http.StatusBadRequest, 1, err.Error(), nil)
		return
	}

	h.logger.Info("UpdateCollection attempted", zap.Any("collectionId", collection.CollectionId))

	err := h.collectionService.UpdateCollection(collection)

	if err != nil {
		h.logger.Error("UpdateCollection failed", zap.Any("collectionId", collection.CollectionId), zap.Any("collectionId", collection.CollectionId), zap.Any("error", err))
		resp.HandleError(ctx, http.StatusInternalServerError, 1, err.Error(), nil)
		return
	}

	resp.HandleSuccess(ctx, nil)
}

// 删除收藏夹
func (h *collectionHandler) DelCollection(ctx *gin.Context) {
	var params struct {
		CollectionId int `json:"collectionId"`
	}

	if err := ctx.ShouldBind(&params); err != nil {
		h.logger.Warn("DelCollection", zap.Any("error", err))
		resp.HandleError(ctx, http.StatusBadRequest, 1, err.Error(), nil)
		return
	}

	h.logger.Info("DelCollection attempted", zap.Any("collectionId", params.CollectionId))

	err := h.collectionService.DelCollection(params.CollectionId)

	if err != nil {
		h.logger.Error("DelCollection failed", zap.Any("collectionId", params.CollectionId), zap.Any("error", err))
		resp.HandleError(ctx, http.StatusInternalServerError, 1, err.Error(), nil)
		return
	}

	resp.HandleSuccess(ctx, nil)
}
